package com.android.dof;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener; 
import android.content.SharedPreferences;
import android.content.Intent;

import java.text.DecimalFormat;

public class DOF extends Activity  implements SeekBar.OnSeekBarChangeListener{
	SeekBar mFocalSeekBar;
    SeekBar mApertureSeekBar;
    SeekBar mDistanceSeekBar;
    int mCurrentFocal;
    int mCurrentAperture;
    int mCurrentDistance;
    
    TextView mFocalText;
    TextView mApertureText;
    TextView mDistanceText;
    TextView mHyperfocal;
    TextView mMin;
    TextView mMax;
    TextView mDof;
    Button mOptions;
    TextView mExit;

    private SharedPreferences mPrefs;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mPrefs = getSharedPreferences("dof", 0);
        mCurrentFocal = mPrefs.getInt("focal", 4);
        mCurrentAperture = mPrefs.getInt("aperture", 14);
        mCurrentDistance = mPrefs.getInt("distance", 120);
        setContentView(R.layout.main);
        
        // Text entries
        mHyperfocal = (TextView)findViewById(R.id.hyperfocal);
        mMin = (TextView)findViewById(R.id.min);
        mMax = (TextView)findViewById(R.id.max);
        mDof = (TextView)findViewById(R.id.dof);
        
        // Focal widgets
        mFocalSeekBar = (SeekBar)findViewById(R.id.focal_seek);
        mFocalSeekBar.setMax(mFocal.length);
        mFocalSeekBar.setOnSeekBarChangeListener(this);
        mFocalText = (TextView)findViewById(R.id.focal_text);
        
        // Aperture widgets
        mApertureSeekBar = (SeekBar)findViewById(R.id.aperture_seek);
        mApertureSeekBar.setMax(mAperture.length);
        mApertureSeekBar.setOnSeekBarChangeListener(this);
        mApertureText = (TextView)findViewById(R.id.aperture_text);
        
        // Distance widget
        mDistanceSeekBar = (SeekBar)findViewById(R.id.distance_seek);
        mDistanceSeekBar.setOnSeekBarChangeListener(this);
        mDistanceText = (TextView)findViewById(R.id.distance_text);
        
        mOptions = (Button)findViewById(R.id.boptions);
        
        mOptions.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		if (v == mOptions) {
        	        startActivity(new Intent("com.android.dof.SETTINGS"));
        		}
        	}
        }
        		
        );
    }
    
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
    	if (seekBar == mApertureSeekBar) {
    		mCurrentAperture = progress;
    		mApertureText.setText("Aperture: " + mAperture[progress][1] + " ");
    	}
			
		if (seekBar == mFocalSeekBar) {
			mCurrentFocal = progress;
			mFocalText.setText("Focal: " + mFocal[mCurrentFocal][1] + " mm");
		}
			
		if (seekBar == mDistanceSeekBar) {
			mCurrentDistance = progress;
			mDistanceText.setText("Distance: " + (double)progress/100. + " m");
		}
			
		 Double CoC = Settings.getCoc();
		 Double focal = Double.parseDouble(mFocal[mFocalSeekBar.getProgress()][0]);
		 Double aperture = Double.parseDouble(mAperture[mApertureSeekBar.getProgress()][0]);
		 Double hyperFocal = (focal * focal) / (aperture * CoC) + focal;
		 
		 Double dofNear = 0.0;
		 Double dofFar  = 0.0;
		 Double distance;
		 int units = 1;

		 distance = new Double(mDistanceSeekBar.getProgress() * 10);
		 dofNear = ((hyperFocal - focal) * distance) / (hyperFocal + distance - (2*focal));

		// Prevent 'divide by zero' when calculating far distance.
		 if ( (hyperFocal - distance) <= 0.00001)
			 dofFar = 10000000.0; // set infinity at 10000m
		 else
			 dofFar = ((hyperFocal-focal) * distance) / (hyperFocal - distance);
		// Calculate percentage of DOF in front and behind the subject.
//		 dofNearPercent = (distance - dofNear)/(dofFar-dofNear) * 100.0;
//		 dofFarPercent = (dofFar - distance)/(dofFar-dofNear) * 100.0;
		// Convert depth of field numbers to proper units
		 dofNear = dofNear / 1000.0 / units;
		 dofFar  = dofFar / 1000.0 / units;

		 // convert hyperfocal distance to proper units
		 hyperFocal = hyperFocal / 1000.0 / units;
			 

			//Display results
		 DecimalFormat df = new DecimalFormat("#.##");
		 mHyperfocal.setText("Hyperfocal: " + df.format(hyperFocal) + " m ");
		 
		 mMin.setText("Min: " + df.format(dofNear) + " m");
		 
		 if (dofFar == 10000.0 ) {// set infinity at 10000m
			 mMax.setText("Max: Infinity ");
			 mDof.setText("DOF: Infinity ");
		 } else {
			 mMax.setText("Max: " + df.format(dofFar) + " m");
			 mDof.setText("DOF: " + df.format(dofFar - dofNear) + " m");
		 }

    }
    private void savePrefs() {
        SharedPreferences.Editor ed = mPrefs.edit();
        
        ed.putInt("focal", mCurrentFocal);
        ed.putInt("aperture", mCurrentAperture);
        ed.putInt("distance", mCurrentDistance);
        ed.commit();
    }
    
    protected void onPause() {
        super.onPause();
        savePrefs();
    }

     public void onStartTrackingTouch(SeekBar seekBar) {
     }

     public void onStopTrackingTouch(SeekBar seekBar) {
     }
     


     String[][] mFocal = {
 			{"3", "3"},
 			{"3.6", "3.6"},

 			{"4", "4"},
 			{"4.3", "4.3"},
 			{"4.5", "4.5"},
 			{"4.6", "4.6"},
 			{"4.7", "4.7"},
 			{"4.75", "4.75"},

 			{"4.8", "4.8"},
 			{"5.0", "5"},
 			{"5.1", "5.1"},
 			{"5.2", "5.2"},
 			{"5.3", "5.3"},
 			{"5.4", "5.4"},

 			{"5.5", "5.5"},
 			{"5.6", "5.6"},
 			{"5.7", "5.7"},
 			{"5.8", "5.8"},
 			{"5.83", "5.83"},
 			{"5.9", "5.9"},

 			{"6.0", "6"},
 			{"6.1", "6.1"},
 			{"6.11", "6.11"},
 			{"6.2", "6.2"},
 			{"6.3", "6.3"},
 			{"6.32", "6.32"},

 			{"6.33", "6.33"},
 			{"6.4", "6.4"},
 			{"6.5", "6.5"},
 			{"6.6", "6.6"},
 			{"6.7", "6.7"},
 			{"6.8", "6.8"},

 			{"6.9", "6.9"},
 			{"7.0", "7"},
 			{"7.1", "7.1"},
 			{"7.15", "7.15"},
 			{"7.2", "7.2"},
 			{"7.25", "7.25"},

 			{"7.3", "7.3"},
 			{"7.4", "7.4"},
 			{"7.5", "7.5"},
 			{"7.51", "7.51"},
 			{"7.6", "7.6"},
 			{"7.7", "7.7"},

 			{"7.8", "7.8"},
 			{"7.85", "7.85"},
 			{"7.9", "7.9"},
 			{"8.0", "8"},
 			{"8.2", "8.2"},
 			{"8.3", "8.3"},

 			{"8.5", "8.5"},
 			{"8.9", "8.9"},
 			{"9.0", "9"},
 			{"9.2", "9.2"},
 			{"9.7", "9.7"},
 			{"9.9", "9.9"},

 			{"10.0", "10"},
 			{"10.5", "10.5"},
 			{"10.8", "10.8"},
 			{"11.0", "11"},
 			{"11.6", "11.6"},
 			{"12.0", "12"},

 			{"12.5", "12.5"},
 			{"12.6", "12.6"},
 			{"13.0", "13"},
 			{"13.2", "13.2"},
 			{"13.5", "13.5"},
 			{"14.0", "14"},

 			{"14.1", "14.1"},
 			{"14.3", "14.3"},
 			{"14.4", "14.4"},
 			{"14.9", "14.9"},
 			{"15.0", "15"},
 			{"15.1", "15.1"},

 			{"15.3", "15.3"},
 			{"15.5", "15.5"},
 			{"15.6", "15.6"},
 			{"16.0", "16"},
 			{"16.2", "16.2"},
 			{"16.5", "16.5"},

 			{"16.6", "16.6"},
 			{"16.8", "16.8"},
 			{"17.0", "17"},
 			{"17.1", "17.1"},
 			{"17.3", "17.3"},
 			{"17.4", "17.4"},

 			{"18.0", "18"},
 			{"18.3", "18.3"},
 			{"18.6", "18.6"},
 			{"18.9", "18.9"},
 			{"19.0", "19"},
 			{"19.2", "19.2"},

 			{"19.5", "19.5"},
 			{"19.8", "19.8"},
 			{"20.0", "20"},
 			{"20.1", "20.1"},
 			{"20.3", "20.3"},
 			{"20.7", "20.7"},

 			{"21.0", "21"},
 			{"21.3", "21.3"},
 			{"21.4", "21.4"},
 			{"21.6", "21.6"},
 			{"21.8", "21.8"},
 			{"21.9", "21.9"},

 			{"22.0", "22"},
 			{"22.2", "22.2"},
 			{"22.5", "22.5"},
 			{"22.8", "22.8"},
 			{"22.9", "22.9"},
 			{"23.0", "23"},

 			{"23.1", "23.1"},
 			{"23.2", "23.2"},
 			{"23.4", "23.4"},
 			{"23.5", "23.5"},
 			{"23.7", "23.7"},
 			{"24.0", "24"},

 			{"24.3", "24.3"},
 			{"24.9", "24.9"},
 			{"25.0", "25"},
 			{"25.2", "25.2"},
 			{"25.8", "25.8"},
 			{"26.0", "26"},

 			{"26.3", "26.3"},
 			{"26.4", "26.4"},
 			{"27.0", "27"},
 			{"28.0", "28"},
 			{"28.46", "28.4"},
 			{"28.6", "28.6"},

 			{"28.8", "28.8"},
 			{"29.0", "29"},
 			{"29.2", "29.2"},
 			{"30.0", "30"},
 			{"31.0", "31"},
 			{"31.4", "31.4"},

 			{"32.0", "32"},
 			{"33.0", "33"},
 			{"34.0", "34"},
 			{"34.8", "34.8"},
 			{"35.0", "35"},
 			{"35.5", "35.5"},

 			{"36.0", "36"},
 			{"36.6", "36.6"},
 			{"37.0", "37"},
 			{"37.5", "37.5"},
 			{"38.0", "38"},
 			{"39.0", "39"},

 			{"39.5", "39.5"},
 			{"40.0", "40"},
 			{"41.0", "41"},
 			{"42.0", "42"},
 			{"43.0", "43"},
 			{"44.7", "44.7"},

 			{"45.0", "45"},
 			{"46.0", "46"},
 			{"46.8", "46.8"},
 			{"47.0", "47"},
 			{"48.0", "48"},
 			{"48.5", "48.5"},

 			{"50.0", "50"},
 			{"50.8", "50.8"},
 			{"50.9", "50.9"},
 			{"51.0", "51.0"},
 			{"51.2", "51.2"},
 			{"51.6", "51.6"},

 			{"52.0", "52"},
 			{"53.0", "53"},
 			{"54.0", "54"},
 			{"55.0", "55"},
 			{"55.2", "55.2"},
 			{"56.0", "56"},

 			{"58.0", "58"},
 			{"59.0", "59"},
 			{"60.0", "60"},
 			{"61.0", "61"},
 			{"63.0", "63"},
 			{"63.2", "63.2"},

 			{"65.0", "65"},
 			{"66.0", "66"},
 			{"66.7", "66.7"},
 			{"69.0", "69"},
 			{"69.9", "69.9"},
 			{"70.0", "70"},

 			{"71.2", "71.2"},
 			{"71.5", "71.5"},
 			{"72.0", "72"},
 			{"75.0", "75"},
 			{"76.0", "76"},
 			{"80.0", "80"},

 			{"85.0", "85"},
 			{"86.0", "86"},
 			{"88.8", "88.8"},
 			{"89.0", "89"},
 			{"90.0", "90"},
 			{"95.0", "95"},

 			{"100.0", "100"},
 			{"101.0", "101"},
 			{"102.0", "102"},
 			{"103.0", "103"},
 			{"104.0", "104"},
 			{"105.0", "105"},

 			{"106.0", "106"},
 			{"108.0", "108"},
 			{"110.0", "110"},
 			{"111.0", "111"},
 			{"114.0", "114"},
 			{"115.0", "115"},

 			{"117.0", "117"},
 			{"120.0", "120"},
 			{"121.0", "121"},
 			{"123.0", "123"},
 			{"125.0", "125"},
 			{"127.0", "127"},

 			{"130.0", "130"},
 			{"135.0", "135"},
 			{"140.0", "140"},
 			{"145.0", "145"},
 			{"150.0", "150"},
 			{"152.0", "152"},

 			{"155.0", "155"},
 			{"160.0", "160"},
 			{"165.0", "165"},
 			{"168.0", "168"},
 			{"170.0", "170"},
 			{"180.0", "180"},

 			{"190.0", "190"},
 			{"200.0", "200"},
 			{"203.0", "203"},
 			{"205.0", "205"},
 			{"210.0", "210"},
 			{"215.0", "215"},

 			{"240.0", "240"},
 			{"250.0", "250"},
 			{"254.0", "254"},
 			{"255.0", "255"},
 			{"260.0", "260"},
 			{"270.0", "270"},

 			{"280.0", "280"},
 			{"300.0", "300"},
 			{"305.0", "305"},
 			{"320.0", "320"},
 			{"350.0", "350"},
 			{"355.0", "355"},

 			{"360.0", "360"},
 			{"375.0", "375"},
 			{"380.0", "380"},
 			{"400.0", "400"},
 			{"420.0", "420"},
 			{"450.0", "450"},

 			{"480.0", "480"},
 			{"500.0", "500"},
 			{"600.0", "600"},
 			{"610.0", "610"},
 			{"720.0", "720"},
 			{"800.0", "800"},

 			{"1000.0", "1000"},
 			{"1200.0", "1200"},
     };
 	String[][] mAperture = {
 			{"1", "f/1"},
 			{"1.189207", "f/1.2"},
 			{"1.414214", "f/1.4"},
 			{"1.587401", "f/1.6"},

 			{"1.681793", "f/1.7"},
 			{"1.781797", "f/1.8"},
 			{"2.000000", "f/2"},
 			{"2.244924", "f/2.2"},
 			{"2.378414", "f/2.4"},
 			{"2.519842", "f/2.5"},

 			{"2.828427", "f/2.8"},
 			{"3.174802", "f/3.2"},
 			{"3.363586", "f/3.4"},
 			{"3.563595", "f/3.6"},
 			{"4.000000", "f/4"},
 			{"4.489848", "f/4.5"},

 			{"4.756828", "f/4.8"},
 			{"5.039684", "f/5"},
 			{"5.656854", "f/5.6"},
 			{"6.349604", "f/6.4"},
 			{"6.727171", "f/6.7"},
 			{"7.127190", "f/7.1"},

 			{"8.000000", "f/8"},
 			{"8.979696", "f/9"},
 			{"9.513657", "f/9.5"},
 			{"10.07937", "f/10"},
 			{"11.313708", "f/11"},
 			{"12.699208", "f/12.7"},

 			{"13.454343", "f/13.5"},
 			{"14.254379", "f/14.3"},
 			{"16.000000" , "f/16"},
 			{"17.959393", "f/18"},
 			{"19.027314", "f/19"},
 			{"20.158737", "f/20"},

 			{"22.627417", "f/22"},
 			{"25.398417", "f/25"},
 			{"26.908685", "f/27"},
 			{"28.508759", "f/28"},
 			{"32", "f/32"},
 			{"45.254834", "f/45"},

 			{"64", "f/64"},

 			
 	};


}